//SPDX-FileCopyrightText: Copyright 2022-2024 深圳市同心圆网络有限公司
//SPDX-License-Identifier: GPL-3.0-only

import { invoke } from '@tauri-apps/api/tauri';

export type ASSET_OWNER_TYPE = number;
export const ASSET_OWNER_MEMBER: ASSET_OWNER_TYPE = 0;
export const ASSET_OWNER_DEPART_MENT: ASSET_OWNER_TYPE = 1;


export type AssetCateInfo = {
    asset_cate_id: string;
    cate_name: string;
    asset_count: number;
};

export type UserPerm = {
    can_update: boolean;
    can_move_in_depart_ment: boolean;
    can_move_global: boolean;
    can_remove: boolean;
};

export type AssetInfo = {
    asset_id: string;
    asset_no: string;
    asset_desc: string;
    owner_type: ASSET_OWNER_TYPE;
    owner_id: string;
    owner_name: string;
    asset_cate_id: string;
    cate_name: string;
    create_time: number;
    create_user_id: string;
    create_display_name: string;
    create_logo_uri: string;
    update_time: number;
    update_user_id: string;
    update_display_name: string;
    update_logo_uri: string;
    user_perm: UserPerm;
}

export type AddAssetCateRequest = {
    session_id: string;
    org_id: string;
    cate_name: string;
};

export type AddAssetCateResponse = {
    code: number;
    err_msg: string;
    asset_cate_id: string;
};

export type ListAssetCateRequest = {
    session_id: string;
    org_id: string;
};

export type ListAssetCateResponse = {
    code: number;
    err_msg: string;
    cate_list: AssetCateInfo[];
};

export type UpdateAssetCateRequest = {
    session_id: string;
    org_id: string;
    asset_cate_id: string;
    cate_name: string;
};

export type UpdateAssetCateResponse = {
    code: number;
    err_msg: string;
};

export type RemoveAssetCateRequest = {
    session_id: string;
    org_id: string;
    asset_cate_id: string;
};

export type RemoveAssetCateResponse = {
    code: number;
    err_msg: string;
};

export type AddAssetRequest = {
    session_id: string;
    org_id: string;
    asset_cate_id: string;
    asset_no: string;
    asset_desc: string;
    owner_type: ASSET_OWNER_TYPE;
    owner_id: string;
};

export type AddAssetResponse = {
    code: number;
    err_msg: string;
    asset_id: string;
};

export type ListMemberAssetRequest = {
    session_id: string;
    org_id: string;
    member_user_id: string;
};

export type ListMemberAssetResponse = {
    code: number;
    err_msg: string;
    asset_list: AssetInfo[];
};

export type ListDepartMentAssetRequest = {
    session_id: string;
    org_id: string;
    depart_ment_id: string;
};

export type ListDepartMentAssetResponse = {
    code: number;
    err_msg: string;
    asset_list: AssetInfo[];
};

export type UpdateBaseForAssetRequest = {
    session_id: string;
    org_id: string;
    asset_id: string;
    asset_no: string;
    asset_desc: string;
};

export type UpdateBaseForAssetResponse = {
    code: number;
    err_msg: string;
};

export type UpdateCateForAssetRequest = {
    session_id: string;
    org_id: string;
    asset_id: string;
    asset_cate_id: string;
};

export type UpdateCateForAssetResponse = {
    code: number;
    err_msg: string;
};

export type MoveAssetRequest = {
    session_id: string;
    org_id: string;
    asset_id: string;
    owner_type: ASSET_OWNER_TYPE;
    owner_id: string;
};

export type MoveAssetResponse = {
    code: number;
    err_msg: string;
};

export type RemoveAssetRequest = {
    session_id: string;
    org_id: string;
    asset_id: string;
};

export type RemoveAssetResponse = {
    code: number;
    err_msg: string;
};

//增加资产类别
export async function add_asset_cate(request: AddAssetCateRequest): Promise<AddAssetCateResponse> {
    const cmd = 'plugin:org_asset_api|add_asset_cate';
    console.log(`%c${cmd}`, 'color:#0f0;', request);
    return invoke<AddAssetCateResponse>(cmd, {
        request,
    });
}

//列出资产类别
export async function list_asset_cate(request: ListAssetCateRequest): Promise<ListAssetCateResponse> {
    const cmd = 'plugin:org_asset_api|list_asset_cate';
    console.log(`%c${cmd}`, 'color:#0f0;', request);
    return invoke<ListAssetCateResponse>(cmd, {
        request,
    });
}

//更新资产类别
export async function update_asset_cate(request: UpdateAssetCateRequest): Promise<UpdateAssetCateResponse> {
    const cmd = 'plugin:org_asset_api|update_asset_cate';
    console.log(`%c${cmd}`, 'color:#0f0;', request);
    return invoke<UpdateAssetCateResponse>(cmd, {
        request,
    });
}

//删除资产类别
export async function remove_asset_cate(request: RemoveAssetCateRequest): Promise<RemoveAssetCateResponse> {
    const cmd = 'plugin:org_asset_api|remove_asset_cate';
    console.log(`%c${cmd}`, 'color:#0f0;', request);
    return invoke<RemoveAssetCateResponse>(cmd, {
        request,
    });
}

//增加资产
export async function add_asset(request: AddAssetRequest): Promise<AddAssetResponse> {
    const cmd = 'plugin:org_asset_api|add_asset';
    console.log(`%c${cmd}`, 'color:#0f0;', request);
    return invoke<AddAssetResponse>(cmd, {
        request,
    });
}

//列出成员资产
export async function list_member_asset(request: ListMemberAssetRequest): Promise<ListMemberAssetResponse> {
    const cmd = 'plugin:org_asset_api|list_member_asset';
    console.log(`%c${cmd}`, 'color:#0f0;', request);
    return invoke<ListMemberAssetResponse>(cmd, {
        request,
    });
}

//列出部门资产(包含成员资产)
export async function list_depart_ment_asset(request: ListDepartMentAssetRequest): Promise<ListDepartMentAssetResponse> {
    const cmd = 'plugin:org_asset_api|list_depart_ment_asset';
    console.log(`%c${cmd}`, 'color:#0f0;', request);
    return invoke<ListDepartMentAssetResponse>(cmd, {
        request,
    });
}

//更新资产(基本信息)
export async function update_base_for_asset(request: UpdateBaseForAssetRequest): Promise<UpdateBaseForAssetResponse> {
    const cmd = 'plugin:org_asset_api|update_base_for_asset';
    console.log(`%c${cmd}`, 'color:#0f0;', request);
    return invoke<UpdateBaseForAssetResponse>(cmd, {
        request,
    });
}

//更新资产(类别)
export async function update_cate_for_asset(request: UpdateCateForAssetRequest): Promise<UpdateCateForAssetResponse> {
    const cmd = 'plugin:org_asset_api|update_cate_for_asset';
    console.log(`%c${cmd}`, 'color:#0f0;', request);
    return invoke<UpdateCateForAssetResponse>(cmd, {
        request,
    });
}

//移动资产
export async function move_asset(request: MoveAssetRequest): Promise<MoveAssetResponse> {
    const cmd = 'plugin:org_asset_api|move_asset';
    console.log(`%c${cmd}`, 'color:#0f0;', request);
    return invoke<MoveAssetResponse>(cmd, {
        request,
    });
}

//除资产
export async function remove_asset(request: RemoveAssetRequest): Promise<RemoveAssetResponse> {
    const cmd = 'plugin:org_asset_api|remove_asset';
    console.log(`%c${cmd}`, 'color:#0f0;', request);
    return invoke<RemoveAssetResponse>(cmd, {
        request,
    });
}